package web.controller.address_telefon;

import hibernate.pojo.User;
import hibernate.pojo_address_telefon.Telephone;
import hibernate.pojo_address_telefon.TelephoneBook;
import hibernate.util.HibernateUtil;

import java.util.ArrayList;
import java.util.List;

import javax.faces.component.UIData;
import javax.faces.context.FacesContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.Transaction;
import org.hibernate.classic.Session;
import org.hibernate.criterion.Restrictions;

import web.controller.LoginController;

public class TelephoneBookController {

	private static final Log logger = LogFactory.getLog(LoginController.class);
	
	private List<TelephoneBook> telephoneBooks;
	private User user;
	private UIData telephoneBooksTable;
	private TelephoneBook telephoneBook;
	private String telephoneBookName;
	private int telephoneBookID;
	private String userFirstName;
	private Boolean deleteConfirmationPopup;
	
	private List<Telephone> telephones;
	
	public TelephoneBookController() {
		// TODO Auto-generated constructor stub
	}

	public void setTelephoneBooks(List<TelephoneBook> telephoneBooks) {
		this.telephoneBooks = telephoneBooks;
	}

	public List<TelephoneBook> getTelephoneBooks() {
		
		try {
			
			FacesContext context = FacesContext.getCurrentInstance();
			user = (User)context.getExternalContext().getSessionMap().get("user");
			
			telephoneBooks = new ArrayList<TelephoneBook>();
			Session session = HibernateUtil.getSessionFactory().openSession();
			Criteria criteria = session.createCriteria(TelephoneBook.class);
			criteria.add(Restrictions.eq("user",user));
			telephoneBooks = criteria.list();
			
		} catch (Exception exception) {
			logger.error("Kullanici listesi database den okunurken hata olustu!",exception);
		}
		
		if(telephoneBooks != null && telephoneBooks.size() > 0){
			return telephoneBooks;
		} else 
			return null;
	}
	
	public String editTelephoneBook(){
		String pageToReturn = "";
		try {
			setTelephoneBook(((TelephoneBook)telephoneBooksTable.getRowData()));
			setTelephoneBookName(getTelephoneBook().getTelephoneBookName());
			
			logger.info("Initial User Values!");
			logger.info(getUserFirstName());
			logger.info(getTelephoneBookID());
			logger.info(getTelephoneBookName());
			//logger.info(get)
			pageToReturn = "showTelephoneBookDetailPage";
		} catch (Exception exception) {
			logger.error("Secilen kullaniciyi getirirken hata olustu!", exception);
		}
		
		return pageToReturn;
		
	}
	
	public String updateTelephoneBookInformation(){
		String pageToRedirect = "";
		
		logger.info("Updated TelephoneBook Values!");
		logger.info(getTelephoneBookName());
				
		try {
			
//			FacesContext context = FacesContext.getCurrentInstance();
//			user = (User)context.getExternalContext().getSessionMap().get("user");
			telephoneBook.setTelephoneBookName(getTelephoneBookName());
			
			Session session = HibernateUtil.getSessionFactory().openSession();
			Transaction transaction = session.beginTransaction();
			session.update(telephoneBook);
			transaction.commit();
		
			pageToRedirect = "updateTelephoneBookInfoSuccess";
		} catch (Exception exception) {
			logger.error("Error Occured During User Info Update :", exception);
		}		
		
		return pageToRedirect;
	}
	
	public String listTelephones(){
		String pageToRedirect = "";
		
		logger.info("Updated TelephoneBook Values!");
		if (getTelephoneBooks().get(0) != null) {
			logger.info(getTelephoneBooks().get(0).getTelephoneBookName());
			setTelephoneBook(getTelephoneBooks().get(0));
		}
		
		try {
			
			Criteria criteria = HibernateUtil.getSessionFactory().openSession().createCriteria(Telephone.class);
			criteria.add(Restrictions.eq("telephoneBook",getTelephoneBook()));

			telephones = criteria.list();
			if( telephones!= null && telephones.size() > 0) {
				FacesContext context = FacesContext.getCurrentInstance();
				String telephoneBookID = Integer.toString(telephones.get(0).getTelephoneBookID());
				//String telephoneBookID2 = ""+telephones.get(0).getTelephoneBookID();
				context.getExternalContext().getSessionMap().put("telephoneBookID", telephoneBookID);
				pageToRedirect = "listTelephonesSuccess";
			}
			else {
				pageToRedirect = "failure";
			}
			
		} catch (Exception exception) {
			logger.error("Error Occured During User Info Update :", exception);
		}		
		
		return pageToRedirect;
	}
	
	public String showDeleteConfirmationPopup() {

		String pageToRedirect = "";
		setTelephoneBook(((TelephoneBook)telephoneBooksTable.getRowData()));
		setDeleteConfirmationPopup(true);
		
		return pageToRedirect;
	}
	
	public void closeDeleteConfirmationPopup() {
		setDeleteConfirmationPopup(false);
	}

	public void setTelephoneBooksTable(UIData telephoneBooksTable) {
		this.telephoneBooksTable = telephoneBooksTable;
	}

	public UIData getTelephoneBooksTable() {
		return telephoneBooksTable;
	}

	public void setTelephoneBook(TelephoneBook telephoneBook) {
		this.telephoneBook = telephoneBook;
	}

	public TelephoneBook getTelephoneBook() {
		return telephoneBook;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public String getTelephoneBookName() {
		return telephoneBookName;
	}

	public void setTelephoneBookName(String telephoneBookName) {
		this.telephoneBookName = telephoneBookName;
	}

	public int getTelephoneBookID() {
		return telephoneBookID;
	}

	public void setTelephoneBookID(int telephoneBookID) {
		this.telephoneBookID = telephoneBookID;
	}

	public String getUserFirstName() {
		return userFirstName;
	}

	public void setUserFirstName(String userFirstName) {
		this.userFirstName = userFirstName;
	}

	public void setDeleteConfirmationPopup(Boolean deleteConfirmationPopup) {
		this.deleteConfirmationPopup = deleteConfirmationPopup;
	}

	public Boolean getDeleteConfirmationPopup() {
		return deleteConfirmationPopup;
	}

	public void setTelephones(List<Telephone> telephones) {
		this.telephones = telephones;
	}

	public List<Telephone> getTelephones() {
		return telephones;
	}
	
}
